﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Web;
using System.Web.Mvc;
using Business;
using Common;
using Dto;
using YaoYunDong.Models;

namespace YaoYunDong.Controllers
{
    public class ActivityController : BaseController
    {
        private readonly ActivityBusiness acivityBusiness = new ActivityBusiness();
        private readonly SportsGroupBusiness groupBusiness = new SportsGroupBusiness();
        private static readonly string _activityNarrowPath = ConfigurationManager.AppSettings["ActivityNarrowPath"];
        private static readonly string _activityOriginalPath = ConfigurationManager.AppSettings["ActivityOriginalPath"];

        public ActionResult CreateActivityBaseInfo(GroupActivityModel model)
        {
            var usersDto = Session["User"] as UsersDto;
            if (usersDto != null)
            {
                var groupDto = groupBusiness.GetGroupById(model.GroupId);
                var activityDto = new GroupActivityDto
                                      {
                                          GaAddress = model.Address,
                                          GaAudit = 1,
                                          GaCity = model.City,
                                          GaCurState = 1,
                                          GaEndDate = model.HidEndDate,
                                          GaEndTime = model.HidEndTime,
                                          GaFee = Convert.ToDecimal(model.Fee),
                                          GaFrontCover = 0,
                                          GaGroupId = model.GroupId,
                                          GaInputTime = DateTime.Now,
                                          GaMode = model.ActiveMode,
                                          GaPayType = model.HidPayType,
                                          GaRemark = model.Content,
                                          GaSponsor = usersDto.UserId,
                                          GaSportId = model.HidSportId,
                                          GaStartDate = model.HidStartDate,
                                          GaStartTime = model.HidEndTime,
                                          GaTitle = model.Title,
                                          GaType = 1
                                      };
                Session["ActivityDto"] = activityDto;
                return RedirectToAction("CreateActivity", "Group", new { code = groupDto.GroupCode });
            }
            return RedirectToAction("Index", "Channel", new { channelCode = "yqzb" });
        }

        public ActionResult CreateGroupActivity(string imgName, int defaultImgId, string[] partners)
        {
            if (Session["User"] != null && Session["ActivityDto"] != null)
            {
                var userDto = Session["User"] as UsersDto;
                var activityDto = Session["ActivityDto"] as GroupActivityDto;
                if (activityDto != null)
                {
                    var annexUploadUtil = new AnnexUploadUtil();
                    var annexDto = new AnnexDto();
                    if (!string.IsNullOrEmpty(imgName))
                    {
                        annexDto = annexUploadUtil.ImageUpload("/TempImage/" + imgName, imgName, 160, 160,
                                                               _activityNarrowPath,
                                                               _activityOriginalPath, true);
                        activityDto.GaFrontCover = annexDto.AnnexID;
                    }
                    else
                    {
                        activityDto.GaFrontCover = defaultImgId;
                    }
                    var activityPartnerDtos = new List<ActivityPartnerDto>();
                    foreach (var partner in partners)
                    {
                        var activityPartnerDto = new ActivityPartnerDto
                                                     {
                                                         ApActivityType = 1,
                                                         ApInvitedTime = DateTime.Now,
                                                         ApMessage = "",
                                                         ApState = 3,
                                                         ApUserId = Convert.ToInt32(partner)
                                                     };
                        activityPartnerDtos.Add(activityPartnerDto);
                    }
                    var b = acivityBusiness.CreateGroupActivity(activityDto, activityPartnerDtos);
                    return RedirectToAction("ActivityList", "Group");
                }

            }
            return RedirectToAction("ActivityList", "Group");
        }
    }
}
